import sys
import json
print("hello")
file = open("generatefile.cpp", "w")


if len(sys.argv) < 2:
    print("请至少传递一个参数")
    sys.exit(1)
#类名 函数名 参数名 返回类型
first_param = sys.argv[1]
print(f"你传递的第一个参数是: {first_param}")

#funcName="TrySetVbia"
#className="MyClass"
#paras =[{    "name":"v",
#    "type":"VarBaseInt"}]
#result = {
#    "type":"VarBaseBool"
#}
# 读取 JSON 文件
with open('data.json', 'r') as f:
    data = json.load(f)
funcName = data["funcName"]
className = data["className"]
paras = data["paras"]
result = data["result"]

    
file.write(f"BlueprintScriptLibrary::NativeMethodPin* Construct{funcName}Method()")
file.write(f"{{\n")
#file.write(f"std::function<void({className}*,")
pre =  f"   std::function<BlueprintScriptLibrary::{result['type']}({className}*"
holders="std::placeholders::_1"
if( len(paras) > 0):
    pre+=","
    holders+=","
index = 0
#for ele in paras:
while index < len(paras):
    pre+= f"BlueprintScriptLibrary::{paras[index]['type']}"
    holders+=f"std::placeholders::_{index+2}"
    print(index)
    if index < len(paras)-1:
        pre+=","
    index += 1
pre+=f")> f=std::bind(&{className}::{funcName},"+holders+f");\n"
file.write(pre)
print(37)

file.write("	BlueprintScriptLibrary::NativeMethodPin* fp = new BlueprintScriptLibrary::NativeMethodPin;\n") 
file.write("    std::vector<BlueprintScriptLibrary::Var*> vbs;\n");
file.write(f"    BlueprintScriptLibrary::Array<BlueprintScriptLibrary::Var> inputArray({len(paras)});\n")
file.write(f"    BlueprintScriptLibrary::Array<BlueprintScriptLibrary::Var> outputArray(1);\n")
file.write(f"    std::function<BlueprintScriptLibrary::Var**(BlueprintScriptLibrary::Object*, BlueprintScriptLibrary::Var**)> func = [f](BlueprintScriptLibrary::Object* obj, BlueprintScriptLibrary::Var** input)->BlueprintScriptLibrary::Var** {{\n")
file.write(f"       {className}* mc = dynamic_cast<{className}*>(obj);\n")

index = 0
while index < len(paras):
    file.write(f"       BlueprintScriptLibrary::{paras[index]['type']}* input{index} = dynamic_cast<BlueprintScriptLibrary::{paras[index]['type']}*>(input[{index}]);\n")
    index += 1
if result['type'] != "void":    
    file.write("	    BlueprintScriptLibrary::Array<BlueprintScriptLibrary::Var> outputArray(1,false);\n")
    #file.write(f"	    BlueprintScriptLibrary::{result['type']}* outRes = new BlueprintScriptLibrary::{result['type']}(false);\n")
    file.write(f"	    BlueprintScriptLibrary::{result['type']}* outRes = NewObj<BlueprintScriptLibrary::{result['type']}>(false);\n")
if result['type'] != "void":
    file.write(f"       *outRes = f(mc, ")
else:
    file.write("        f(mc,")

index = 0   
while index < len(paras):
    file.write(f"*input{index}")
    if index < len(paras)-1:
        file.write(",")
    index += 1
file.write(f" );\n")
if result['type'] != "void":    
    file.write(f"       outputArray.data[0] = (BlueprintScriptLibrary::Var*)outRes;\n")
    file.write(f"       return outputArray.data;\n")
file.write(" 	 };\n")
file.write(f'    fp->Init("{funcName}", inputArray.data, outputArray.data, func);\n')
file.write("     return fp;\n")
file.write("}")
file.close()
    
#file.write(f"std::function<void({className}*, BlueprintScriptLibrary::VarBaseInt)> f = std::bind(&MyClass::{funcName}, std::placeholders::_1, std::placeholders::_2);\n")